package cn.com.huzo.service.manufacture.impl;

import cn.com.huzo.model.entities.manufacture.RequisitionSlipLinesEntity;
import cn.com.huzo.repository.manufacture.RequisitionSlipLinesRepository;
import huzofw.service.impl.GenericManagerImpl;
import cn.com.huzo.service.manufacture.RequisitionSlipLinesManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 领料单明细数据管理服务类实现。
 *
 * @author 吴旭
 */
@Service("RequisitionSlipLinesManager")
public class RequisitionSlipLinesManagerImpl extends GenericManagerImpl<RequisitionSlipLinesEntity, String>
        implements RequisitionSlipLinesManager {

    @Resource(name = "RequisitionSlipLinesRepository")
    private RequisitionSlipLinesRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Override
    public List<RequisitionSlipLinesEntity> fetchLinesByHeaderId(Long headerId) throws Exception {
        return _dao.fetchByHeaderKey(headerId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<RequisitionSlipLinesEntity> supplement(Long headerId, Long productId, Long userId) throws Exception {
        Integer rowsAffected = _dao.supplement(headerId, productId, userId);
        return _dao.fetchByHeaderKey(headerId);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchUpdate(List<RequisitionSlipLinesEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();
        Integer rowsAffected = _dao.batchUpdate(itemList);
        if (rowsAffected > 0) {
            _result.put("Success", true);
            _result.put("LinesList", _dao.fetchByHeaderKey(itemList.get(0).getHeaderId()));
        } else {
            _result.put("Success", false);
        }
        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> delete(Map<String, Object> criterion) throws Exception {
        Map<String, Object> _result = new HashMap();
        // step1：删除
        Integer rowsAffected = _dao.delete(MapUtils.getString(criterion, "LinesId"));

        if (rowsAffected > 0) {
            _result.put("Success", true);
            // step3：审计日志
            // writeAuditLog(MapUtils.getLong(criterion, "DeletedBy"),
            //         "生产管理→领料单明细",
            //         "明细数据（" + MapUtils.getString(criterion, "LinesData") + "）",
            //         "删除明细");
        } else {
            _result.put("Success", false);
        }
        _result.put("LinesList", _dao.fetchByHeaderKey(MapUtils.getLong(criterion, "HeaderId")));

        return _result;
    }
}
